<template>
  <div class="login">
    <div class="login_box">
      <el-row :gutter="24" class="row">
        <el-col :xs="24" :sm="24" :lg="12" class="hidden-md-and-down">
          <img src="@/assets/images/login_left_box.png" class="left_img" />
        </el-col>
        <el-col :xs="24" :sm="24" :lg="12">
          <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
            <h2 class="title">登录</h2>
            <el-form-item prop="username" style="margin-top: 13px;">
              <el-input
                style="font-size: 28px;"
                v-model="loginForm.username"
                type="text"
                auto-complete="off"
                placeholder="账号"
              >
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                v-model="loginForm.password"
                type="password"
                auto-complete="off"
                show-password
                placeholder="密码"
                @keyup.enter.native="handleLogin"
              >
                <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
              </el-input>
            </el-form-item>
            <el-form-item prop="code">
              <el-input
                v-model="loginForm.code"
                auto-complete="off"
                placeholder="验证码"
                style="width: 60%"
                @keyup.enter.native="handleLogin"
              >
              </el-input>
              <div class="login-code">
                <img :src="codeUrl" @click="getCode" class="login-code-img" />
              </div>
            </el-form-item>
            <el-form-item style="width:100%;">
              <el-button
                :loading="loading"
                size="medium"
                type="primary"
                class="submit_btn"
                @click.native.prevent="handleLogin"
              >
                <span v-if="!loading" style="font-size: 18px;">登 录</span>
                <span v-else style="font-size: 18px;">登 录 中...</span>
              </el-button>
            </el-form-item>
            <el-row :gutter="24">
              <el-col :span="12" style="text-align: left;">
                <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;font-size: 14px;"
                  >记住密码</el-checkbox
                >
              </el-col>
              <el-col :span="12" style="text-align: right;">
                <el-link style="color: #3672fe;font-size: 14px;">忘记密码</el-link>
              </el-col>
            </el-row>
          </el-form>
        </el-col>
      </el-row>
    </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2021 南昌工学院</span>
    </div>
  </div>
</template>

<script>
  import { getCodeImg } from '@/api/login'
  import Cookies from 'js-cookie'
  import { encrypt, decrypt } from '@/utils/jsencrypt'
  import 'element-ui/lib/theme-chalk/display.css'

  export default {
    name: 'Login',
    components: {},
    data() {
      return {
        codeUrl: '',
        cookiePassword: '',
        loginForm: {
          username: '1',
          password: 'admin123',
          rememberMe: false,
          code: '',
          uuid: ''
        },
        loginRules: {
          username: [{ required: true, trigger: 'blur', message: '用户名不能为空' }],
          password: [{ required: true, trigger: 'blur', message: '密码不能为空' }],
          code: [{ required: true, trigger: 'change', message: '验证码不能为空' }]
        },
        loading: false,
        redirect: undefined
      }
    },
    watch: {
      $route: {
        handler: function(route) {
          this.redirect = route.query && route.query.redirect
        },
        immediate: true
      }
    },
    created() {
      this.getCode()
      this.getCookie()
    },
    methods: {
      getCode() {
        getCodeImg().then(res => {
          this.codeUrl = 'data:image/gif;base64,' + res.img
          this.loginForm.uuid = res.uuid
        })
      },
      getCookie() {
        const username = Cookies.get('username')
        const password = Cookies.get('password')
        const rememberMe = Cookies.get('rememberMe')
        this.loginForm = {
          username: username === undefined ? this.loginForm.username : username,
          password: password === undefined ? this.loginForm.password : decrypt(password),
          rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
        }
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true
            if (this.loginForm.rememberMe) {
              Cookies.set('username', this.loginForm.username, { expires: 30 })
              Cookies.set('password', encrypt(this.loginForm.password), { expires: 30 })
              Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
            } else {
              Cookies.remove('username')
              Cookies.remove('password')
              Cookies.remove('rememberMe')
            }
            this.$store
              .dispatch('Login', this.loginForm)
              .then(() => {
                this.$router.push({ path: this.redirect || '/' }).catch(() => {})
              })
              .catch(() => {
                this.loading = false
                this.getCode()
              })
          }
        })
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss">
  .login_box {
    width: 1533px;
    height: 717px;
    background: #fff;
    box-shadow: 0px 0px 15px 0px rgba(0, 0, 0, 0.05);
    overflow: hidden;
    border-radius: 10px;

    .left_img {
      background-size: 100% 100%;
    }
    .row {
      height: 100%;
    }
  }

  @media only screen and (max-width: 992px) {
    .login {
      display: block !important;
      padding-top: 100px;
    }
    .login_box {
      height: auto;
      margin: 0 auto;
      width: 95%;
    }
    .login-form {
      box-sizing: border-box !important;
      padding: 10px 10px 0px 10px !important;
    }
  }

  .login {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    min-height: 800px;
    background-image: url('../../assets/images/login-background.jpg');
    background-size: cover;
    position: relative;
    left: 0px;
    top: 0px;
  }

  .title {
    margin: 0px auto 30px auto;
    text-align: center;
    color: #707070;
  }

  .logo_line {
    width: 100%;
    box-sizing: border-box;
    padding-bottom: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    .img {
      width: 169px;
      height: 174px;
    }
  }

  .login-form {
    width: 100%;
    box-sizing: border-box;
    padding: 88px 88px 0px 102px;

    .title {
      line-height: 46px;
      font-size: 35px;
      color: #333;
      text-align: center;
    }
    .el-input {
      height: 89px;
      input {
        padding: 0px 27px;
        font-size: 22px;
        height: 89px;
      }
      .el-input__suffix > .el-input__suffix-inner > i {
        font-size: 22px;
      }
    }
    .input-icon {
      height: 39px;
      width: 14px;
      margin-left: 2px;
    }
  }

  .login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
  }

  .login-code {
    width: 38%;
    height: 89px;
    float: right;
    img {
      width: 100%;
      height: 100%;
      cursor: pointer;
      vertical-align: middle;
    }
  }

  .el-login-footer {
    height: 40px;
    line-height: 40px;
    position: absolute;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #999;
    font-family: Arial;
    font-size: 14px;
    letter-spacing: 1px;
  }

  .login-code-img {
    height: 38px;
  }

  .login-enter-active {
    animation: fadeInDownBig 1s;
  }

  @-webkit-keyframes fadeInDownBig {
    from {
      opacity: 0;
      -webkit-transform: translate3d(0, -2000px, 0);
      transform: translate3d(0, -2000px, 0);
    }

    to {
      opacity: 1;
      -webkit-transform: translate3d(0, 0, 0);
      transform: translate3d(0, 0, 0);
    }
  }

  @keyframes fadeInDownBig {
    from {
      opacity: 0;
      -webkit-transform: translate3d(0, -2000px, 0);
      transform: translate3d(0, -2000px, 0);
    }

    to {
      opacity: 1;
      -webkit-transform: translate3d(0, 0, 0);
      transform: translate3d(0, 0, 0);
    }
  }

  .submit_btn {
    background: linear-gradient(#428dff, #3672fe);
    box-shadow: 0px 0px 20px 0px rgba(52, 115, 254, 0.73);
    margin-top: 46px;
    width: 100%;
    font-size: 18px !important;
    font-weight: bold;
    height: 89px;
    &:hover {
      background: linear-gradient(#428dff, #3672fe);
      opacity: 0.8;
    }
  }
</style>
